<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleCheckBack"
          v-hasPermi="['output:backOutcheck:back']"
        >
          撤回
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-circle-check"
          size="mini"
          :disabled="multiple"
          @click="handleCheck"
          v-hasPermi="['output:backOutcheck:check']"
        >
          审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          size="mini"
          :disabled="multiple"
          @click="listVendorConfirm"
          v-hasPermi="['output:backOutcheck:confirm']"
        >
          批量厂商确认
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['output:backOutStart:export']"
        >
          导出
        </el-button>
      </el-col>
      <HdTagGroup />
      <!-- <Tag type="dot" size="default" color="#c4bec3">作废</Tag> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :data="backOutStartList"
      :height="tableH"
      :cell-style="iCellStyle"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" width="40" type="index" align="center" />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="退货单号"
        :show-overflow-tooltip="true"
        align="center"
        width="135"
        prop="ctmDjbh"
      />
      <el-table-column
        label="退货日期"
        :show-overflow-tooltip="true"
        align="center"
        width="150"
        prop="ctmCtrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.ctmCtrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="退货厂商"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="ctmCsdm"
      >
        <template #default="scope">
          <dict-tag :options="allCsOptions" :value="scope.row.ctmCsdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="物料代码"
        :show-overflow-tooltip="true"
        width="135"
        align="center"
        prop="ctmWldm"
      />
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        width="400"
        prop="itmPmgg"
      />
      <el-table-column
        label="单位"
        :show-overflow-tooltip="true"
        width="60"
        align="center"
        prop="itmDw"
      >
        <template #default="scope">
          <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
        </template>
      </el-table-column>
      <el-table-column
        label="存放位置"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="ctmCwdm"
      >
        <template #default="scope">
          <dict-tag :options="allCwOptions" :value="scope.row.ctmCwdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="退货数量"
        :show-overflow-tooltip="true"
        align="center"
        prop="ctmCksl"
      />
      <el-table-column
        label="退货原因"
        :show-overflow-tooltip="true"
        align="center"
        prop="ctmThyy"
      />
      <el-table-column
        label="撤回原因"
        :show-overflow-tooltip="true"
        align="center"
        prop="ctmNgdesc"
      />
      <el-table-column
        label="退货状态"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="ctmZtbz"
      >
        <template #default="scope">
          <dict-tag :options="ctmZtbzOptions" :value="scope.row.ctmZtbz" />
        </template>
      </el-table-column>
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="ctmJlry"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="ctmJlrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.ctmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="ctmShry"
      />
      <el-table-column
        label="审核日期"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="ctmShrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.ctmShrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="批准人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="ctmPzry"
      />
      <el-table-column
        label="批准日期"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="ctmPzrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.ctmPzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="过账人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="ctmGzry"
      />
      <el-table-column
        label="过账日期"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="ctmGzrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.ctmGzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="80"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            :disabled="scope.row.ctmZtbz != 1 || scope.row.ctmTjflag != 1"
            @click="vendorConfirm(scope.row)"
            v-hasPermi="['output:backOutcheck:confirm']"
          >
            厂商确认
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="680px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="70px"
      >
        <el-col :span="12">
          <el-form-item label="退货单号" prop="ctmDjbh">
            <el-input
              v-model="queryParams.ctmDjbh"
              placeholder="请输入退货单号"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" prop="ctmWldm">
            <el-input
              v-model="queryParams.ctmWldm"
              placeholder="请输入物料代码"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开单日期">
            <el-date-picker
              v-model="daterangeCtmJlrq"
              :picker-options="pickerOptions"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item>
            <RadioGroup v-model="queryParams.ctmZtbz">
              <Radio label="5" style="margin: 0px 30px">未审核</Radio>
              <Radio label="6" style="margin-right: 30px">已审核</Radio>
              <Radio label="4" align="right">全部</Radio>
            </RadioGroup>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="规格描述" prop="itmPmgg">
            <el-input
              v-model="queryParams.itmPmgg"
              placeholder="请输入规格描述"
              clearable
              style="width: 560px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-dialog
      :title="title"
      v-model="csdmOpen"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form :inline="true" v-show="showSearch" label-width="80px">
        <el-col :span="24">
          <el-form-item label="退货日期" prop="ctmThrqs">
            <el-date-picker
              clearable
              size="small"
              v-model="ctmThrqs"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择退货日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="退货厂商" prop="ctmCsdms" required>
            <el-select
              v-model="ctmCsdms"
              placeholder="请选择退货厂商"
              clearable
              filterable
            >
              <el-option
                v-for="item in csOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="csdmOpen = false">取消</el-button>
            <el-button type="primary" @click="handleCsdmConfim">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 撤回 -->
    <Modal v-model="check_remove" draggable scrollable width="300">
      <p slot="header" style="color: #f60; text-align: center">
        <Icon type="ios-information-circle"></Icon>
        <span>确认撤回？</span>
      </p>
      <div style="text-align: center">
        <p>撤回将使单据永久失效，请谨慎操作</p>
        <Input v-model="back_reson" placeholder="撤回理由" style="width: 65%" />
      </div>
      <div slot="footer">
        <Button type="error" size="large" long @click="remove">撤回</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import {
  listBackOutStart,
  vToConfirm,
  checkCtm,
  checkBackCtm,
  exportBackOutStart,
} from "@/api/output/backOutStart";
import { getCs, getAllCs } from "@/api/putStorage/putStorageAdd";
import { getAll } from "@/api/repertory/Storage";

export default {
  name: "BackOutCheck",
  data() {
    return {
      //表格高度
      tableH: document.body.clientHeight - 230,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ctmDjbhs: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      //单位字典
      unitFlagOptions: [],
      //存放位置
      allCwOptions: [],
      //退货厂商
      back_reson: null,
      //退货厂商单号
      vCtmDjbhs: [],
      // 总条数
      total: 0,
      // 退货出库表格数据
      backOutStartList: [],
      //选中的单据
      ctmMstrs: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 撤回对话框
      check_remove: false,
      //选择厂商
      csdmOpen: false,
      //退料单号
      vCtmDjnh: [],
      //退货厂商
      ctmCsdms: null,
      //退货日期
      ctmThrqs: new Date(),
      //厂商字典
      csOptions: [],
      allCsOptions: [],
      // 退货状态字典
      ctmZtbzOptions: [
        { dictValue: "1", dictLabel: "新增", listClass: "default" },
        { dictValue: "2", dictLabel: "审核", listClass: "default" },
        { dictValue: "3", dictLabel: "作废", listClass: "default" },
        { dictValue: "4", dictLabel: "批准", listClass: "default" },
        { dictValue: "6", dictLabel: "过账", listClass: "default" },
      ],
      // 建立日期时间范围
      daterangeCtmJlrq: [
        this.$moment().startOf("month").toDate(),
        this.$moment().add(100, "year").endOf("month").toDate(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        ctmDjbh: null,
        ctmWldm: null,
        ctmJlrq: null,
        itmPmgg: null,
        ctmZtbz: "5",
      },
      // 表单参数
      form: {},
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    getAll().then((response) => {
      this.allCwOptions = response.data;

      for (let item2 of this.allCwOptions) {
        item2.dictLabel = item2.storageDefaultstorage;

        item2.listClass = "default";
        item2.dictValue = item2.storageId + "";
      }
    });
    getCs().then((response) => {
      this.csOptions = response.data;
      for (let item of this.csOptions) {
        item.dictLabel = item.csmCs;
        item.listClass = "default";
        item.dictValue = item.csmCsdm;
      }
    });

    getAllCs().then((response) => {
      this.allCsOptions = response.data;
      for (let item of this.allCsOptions) {
        item.dictLabel = item.csmCsmc;
        item.listClass = "default";
        item.dictValue = item.csmCsdm;
      }
    });
    this.getList();
  },
  methods: {
    /** 查询退货出库列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCtmJlrq && "" != this.daterangeCtmJlrq) {
        this.queryParams.params["beginCtmJlrq"] = this.moment(
          this.daterangeCtmJlrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endCtmJlrq"] = this.moment(
          this.daterangeCtmJlrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listBackOutStart(this.queryParams).then((response) => {
        this.backOutStartList = [];
        this.backOutStartList = this.backOutStartList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 颜色事件 */
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //提交
      if (row.ctmZtbz == "1" && row.ctmTjflag == "1") {
        return "color:#0fac09";
      }
      //过账
      else if (row.ctmZtbz == "6" && row.ctmTjflag == "1") {
        return "color:#bb0331";
      }
      //审核
      else if (row.ctmZtbz == "2" && row.ctmTjflag == "1") {
        return "color:#0323af";
      }
      //批准
      else if (row.ctmZtbz == "4" && row.ctmTjflag == "1") {
        return "color:rgb(195,39,174)";
      }
      //作废
      else if (row.ctmZtbz == "3" && row.ctmTjflag == "1") {
        return "background:#c4bec3";
      }
    },
    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        listBackOutStart(this.queryParams).then((response) => {
          this.backOutStartList = this.backOutStartList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ctmId: null,
        ctmDjbh: null,
        ctmCtrq: null,
        ctmCsdm: null,
        ctmWldm: null,
        ctmCwdm: null,
        ctmCksl: null,
        ctmThyy: null,
        ctmNgdesc: null,
        ctmTjflag: null,
        ctmZtbz: null,
        ctmJlry: null,
        ctmJlrq: null,
        ctmShry: null,
        ctmShrq: null,
        ctmPzry: null,
        ctmPzrq: null,
        ctmGzry: null,
        ctmGzrq: null,
      };
      this.resetForm("form");
    },
    resultTh() {
      //退料单号
      this.vCtmDjnh = [];
      //退货厂商
      this.ctmCsdms = null;
      //退货日期
      this.ctmThrqs = this.moment(new Date()).format("YYYY-MM-DD HH:mm:ss");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.backOutStartList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCtmJlrq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ctmDjbhs = selection.map((item) => item.ctmDjbh);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.ctmMstrs = selection;
    },
    handleSearch() {
      this.reset();
      this.title = "查询退货出库";
      this.openSearch = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      exportBackOutStart(queryParams)
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },

    listVendorConfirm() {
      if (this.check_state()) {
        this.msgError("选中的单据不是未审核状态,无法进行批量厂商确认");
      } else {
        this.csdmOpen = true;
        this.title = "厂商确认";
        this.resultTh();
        this.vCtmDjbhs = this.ctmDjbhs;
      }
    },
    vendorConfirm(row) {
      if (row.ctmZtbz != 1 || row.ctmTjflag != 1) {
        this.msgError("选中的单据不是未审核状态,无法进行厂商确认");
      } else {
        this.csdmOpen = true;
        this.title = "厂商确认";
        this.resultTh();
        this.vCtmDjbhs = row.ctmDjbh;
      }
    },
    handleCsdmConfim() {
      if (!this.ctmCsdms) {
        this.msgError("请选择退货厂商~");
        return;
      }
      vToConfirm(this.vCtmDjbhs, this.ctmCsdms, this.ctmThrqs)
        .then((res) => {
          this.resultTh();
          this.csdmOpen = false;
          this.getList();
          this.msgSuccess("厂商确认成功~");
        })
        .catch(() => {
          this.msgError("厂商确认失败~");
        });
    },
    //检验状态
    check_state() {
      for (let i of this.ctmMstrs) {
        if (i.ctmZtbz != 1 || i.ctmTjflag != 1) {
          return true;
        }
      }
      return false;
    },
    //撤回单据
    handleCheckBack() {
      if (this.check_state()) {
        this.msgError("选中的单据不是未审核状态,无法进行撤回");
      } else {
        this.check_remove = true;
        this.vCtmDjbhs = this.ctmDjbhs;
      }
    },
    //撤回单据
    remove() {
      checkBackCtm(this.vCtmDjbhs, this.back_reson)
        .then((res) => {
          this.check_remove = false;
          this.vCtmDjbhs = [];
          this.back_reson = null;
          this.getList();
          this.msgSuccess("撤回成功~");
        })
        .catch(() => {
          this.msgError("撤回失败~");
        });
    },
    //审核单据
    handleCheck() {
      for (let i of this.ctmMstrs) {
        if (i.ctmCsdm === null || i.ctmCsdm === "") {
          this.msgError("请确认厂商");
          return;
        }
      }
      if (this.check_state()) {
        this.msgError("选中的单据不是未审核状态,无法进行审核");
      } else {
        checkCtm(this.ctmDjbhs)
          .then((res) => {
            this.getList();
            this.msgSuccess("审核成功");
          })
          .catch(() => {
            this.msgError("审核单据失败~");
          });
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.inputName > .el-input__inner {
  height: 20px;
  line-height: 20px;
}
</style>
